/**
 *
 */
package jp.seraph.same.controller;

import java.io.IOException;

import javax.swing.JOptionPane;

import jp.seraph.same.core.EditorMotionSelector;
import jp.seraph.same.model.MotionCollectionModel;
import jp.seraph.same.model.MotionModel;

/**
 *
 */
public class DefaultMotionController implements MotionController {
    public DefaultMotionController(EditorMotionSelector aSelector, MotionCollectionModel aMotionCollectionModel) {
        mSelector = aSelector;
        mMotionCollectionModel = aMotionCollectionModel;
    }

    private EditorMotionSelector mSelector;
    private MotionCollectionModel mMotionCollectionModel;

    /**
     *
     * @see jp.seraph.same.controller.MotionController#addPose(java.lang.String,
     *      java.lang.String)
     */

    public void addPose(String aMotionName, String aPoseName) {
        MotionModel tModel = mMotionCollectionModel.getMotion(aMotionName);
        this.addPose(tModel, aMotionName, aPoseName);
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#addPose(java.lang.String)
     */

    public void addPose(String poseName) {
        MotionModel tModel = mMotionCollectionModel.getSelectedMotion();
        this.addPose(tModel, mMotionCollectionModel.getSelectedMotionName(), poseName);
    }

    private void addPose(MotionModel aModel, String aMotionName, String aPoseName) {
        aModel.addPose(aPoseName);
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#removePose(java.lang.String,
     *      java.lang.String)
     */

    public void removePose(String motionName, String poseName) {
        MotionModel tModel = mMotionCollectionModel.getMotion(motionName);
        tModel.removePose(poseName);
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#removePose(java.lang.String)
     */

    public void removePose(String poseName) {
        MotionModel tModel = mMotionCollectionModel.getSelectedMotion();
        tModel.removePose(poseName);
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#runMotion(java.lang.String)
     */

    public void runMotion(String motionName) {
        mSelector.setMotion(mMotionCollectionModel.getMotion(motionName).toMotion());
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#runMotion()
     */

    public void runMotion() {
        mSelector.setMotion(mMotionCollectionModel.getSelectedMotion().toMotion());
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#saveMotions()
     */

    public void saveMotions() {
        try {
            mMotionCollectionModel.save();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "保存に失敗しました", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#selectMotion(java.lang.String)
     */

    public void selectMotion(String motionName) {
        mMotionCollectionModel.selectMotion(motionName);
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#setMotion(java.lang.String,
     *      jp.seraph.same.model.MotionModel)
     */

    public void setMotion(String aMotionName, MotionModel aMotion) {
        mMotionCollectionModel.setMotion(aMotionName, aMotion);
        this.saveMotions();
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#setMotion(java.lang.String)
     */

    public void setMotion(String aMotinName) {
        this.setMotion(aMotinName, mMotionCollectionModel.getSelectedMotion());
    }

    /**
     *
     * @see jp.seraph.same.controller.MotionController#removeMotion(java.lang.String)
     */

    public void removeMotion(String aMotionName) {
        mMotionCollectionModel.removeMotion(aMotionName);
    }
}
